
import org.cgfy.generator.ScrewDriver;
import org.cgfy.generator.constant.MySQL;
import org.cgfy.generator.constant.Oracle;
import org.cgfy.generator.models.SdClassContent;
import org.cgfy.generator.models.SdTemplate;
import org.cgfy.generator.options.ScrewDriverOptions;
import org.cgfy.generator.options.SdDatabaseOptions;
import utils.PropertiesUtil;
import java.util.HashMap;
import java.util.Map;
public class Main {
    //远程
    public static final String jdbcUrl = PropertiesUtil.getDbValue("jdbcUrl");
    public static final String jdbcUser = PropertiesUtil.getDbValue("jdbcUser");
    public static final String jdbcPassword = PropertiesUtil.getDbValue("jdbcPassword");
    public static final String tableName =  PropertiesUtil.getDbValue("tableName");
    public static final String javaFileName =  PropertiesUtil.getDbValue("javaFileName");
    public static final String packageUrl = PropertiesUtil.getDbValue("packageUrl");
    public static final String databaseType = PropertiesUtil.getDbValue("databaseType");
    public static void main(String[] args){
        try {
            String prePackageUrl="ibp.ioc";//前缀包路径(不变)
            String modelPackageUrl="ibp.dto";//model包路径(不变)
            String javaTemplatePath="mybatis";//(不变)
            String httlTemplatePath="page";//(不变)
            Map<String, SdTemplate> templates = new HashMap<String, SdTemplate>();
            String outputPath=System.getProperty("user.dir");
            System.setProperty("user.dir",System.getProperty("user.dir")+"/templates");
            //java生成
            templates.put("entity", new SdTemplate()
                    .setPath(javaTemplatePath)
                    .setFile("EntityLombok.ftl")//Lombok模板为EntityLombok.ftl
                    .setPackageName(modelPackageUrl+"."+packageUrl)
                    .setClassName(javaFileName+"DTO"));
            templates.put("dao", new SdTemplate()
                    .setPath(javaTemplatePath)
                    .setFile("Dao.ftl")
                    .setPackageName(prePackageUrl+".dao."+packageUrl)
                    .setClassName(javaFileName + "DAO"));
            templates.put("mapper", new SdTemplate()
                    .setPath(javaTemplatePath)
                    .setFile("MySqlMapper.ftl")
                    .setPackageName(prePackageUrl+".dao."+packageUrl)
                    //.setSourceFolder("src/main/resources")
                    .setClassName(javaFileName + "DAO")
                    .setSuffix(".xml")
            );
            templates.put("service", new SdTemplate()
                    .setPath(javaTemplatePath)
                    .setFile("Service.ftl")
                    .setPackageName(prePackageUrl+".service."+packageUrl)
                    .setClassName(javaFileName + "Service"));
            templates.put("serviceImpl", new SdTemplate()
                    .setPath(javaTemplatePath)
                    .setFile("ServiceImpl.ftl")
                    .setPackageName(prePackageUrl+".service."+packageUrl+".impl")
                    .setClassName(javaFileName + "ServiceImpl"));
            templates.put("controller", new SdTemplate()
                    .setPath(javaTemplatePath)
                    .setFile("Controller.ftl")
                    .setPackageName(prePackageUrl+".controller."+packageUrl)
                    .setClassName(javaFileName + "Controller"));
            //页面生成
            templates.put("add", new SdTemplate()
                    .setPath(httlTemplatePath)
                    .setFile("Add.ftl")
                    .setPackageName(packageUrl)
                    .setClassName(tableName + "_add")
                    .setSuffix(".httl")
            );
            templates.put("modify", new SdTemplate()
                    .setPath(httlTemplatePath)
                    .setFile("Modify.ftl")
                    .setPackageName(packageUrl)
                    .setClassName(tableName + "_modify")
                    .setSuffix(".httl")
            );
            templates.put("disp", new SdTemplate()
                    .setPath(httlTemplatePath)
                    .setFile("Disp.ftl")
                    .setPackageName(packageUrl)
                    .setClassName(tableName + "_disp")
                    .setSuffix(".httl")
            );

            templates.put("list", new SdTemplate()
                    .setPath(httlTemplatePath)
                    .setFile("List.ftl")
                    .setPackageName(packageUrl)
                    .setClassName(tableName + "_list")
                    .setSuffix(".httl")
            );
            SdDatabaseOptions databaseOptions=null;
            // 数据库连接信息
            if("mysql".equals(databaseType)){
                databaseOptions = new SdDatabaseOptions(MySQL.MYSQL_8_DERVER, jdbcUrl,jdbcUser,jdbcPassword);

            }else if ("oracle".equals(databaseType)){
                databaseOptions = new SdDatabaseOptions(Oracle.ORACLE_DERVER, jdbcUrl);
                databaseOptions.setUser(jdbcUser);
                databaseOptions.setPassword(jdbcPassword);
            }
            // 初始化执行工具
            ScrewDriver screwDriver = ScrewDriver.instance(new ScrewDriverOptions(databaseOptions).setOutputPath(outputPath+"/GENERATE/"));
            // 读取表信息
            SdClassContent content = screwDriver.readTable(tableName);
            // 创建代码
            screwDriver.createCode(content,templates);
            System.out.println("生成成功!");
        }catch (Exception e){
            System.out.println("生成失败,错误信息:"+e.getMessage());
            e.printStackTrace();
        }

    }
}
